package cn.ydxiaoshuai.drools.utils;

import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.text.MessageFormat;


/**
 * @author 小帅丶
 * @className SendMailUtil
 * @Description 发送HTML邮件
 * @Date 2021-06-09-14:10
 **/
@Component
@Slf4j
public class SendMailUtil {
    @Autowired
    private JavaMailSender mailSender;
    /** 发件人邮箱 */
    @Value("${spring.mail.username}")
    private String form;
    /** 发件人别称 */
    @Value("${nickname}")
    private String nickName;

    /**
     * @Description 发送邮件
     * @param subject - 主题
     * @param userNickName - 收件人昵称
     * @param content - 内容
     * @param mails   - 邮件地址
     * @return void
     * @Author 小帅丶
     * @Date 2021-06-09 14:21
     **/
    public void sendHtmlMail(String subject, String userNickName, String content, String mails[]) throws IOException {
        sendHtmlMail(subject, buildContent(userNickName, content), mails, true);
    }

    /**
     * @Description 发送邮件
     * @param subject - 主题
     * @param content - 内容
     * @param mails   - 邮件地址
     * @param isHtml  - 是否是HTML
     * @return void
     * @Author 小帅丶
     * @Date 2021-06-09 14:21
     **/
    public void sendHtmlMail(String subject, String content, String mails[], boolean isHtml) {
//        JavaMailSender mailSender = new JavaMailSenderImpl();
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(form, nickName, CharsetUtil.UTF_8));
            helper.setTo(mails);
            helper.setSubject(subject);
            helper.setText(content, isHtml);
            mailSender.send(message);
            log.info("发送成功");
        } catch (Exception e) {
            log.info("发送失败{}", e.getMessage());
        }
    }


    /**
     * @Description 读取HTML模板并替换相关内容
     * @param userNickName - 用户昵称
     * @param contentText  - 邮件内容
     * @return java.lang.String
     * @Author 小帅丶
     * @Date 2021-06-09 14:48
     **/
    public String buildContent(String userNickName, String contentText) throws IOException {
        //加载邮件html模板
        Resource resource = new ClassPathResource("/mailtemplate/ydxiaoshuai.html");
        InputStream inputStream = resource.getInputStream();
        BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            System.out.println("读取文件失败，" + e.getMessage());
        } finally {
            inputStream.close();
            fileReader.close();
        }
        //填充html模板中的2个参数
        String htmlText = MessageFormat.format(buffer.toString(), userNickName, contentText);
        return htmlText;
    }
}
